package com.lanxin.hr.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lanxin.hr.entity.ZzAdjustment;
import com.lanxin.hr.service.IZzAdjustmentService;
import com.lanxin.hr.service.IZzEmployeeService;
import com.lanxin.hr.vo.AdjustmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-30
 */
@Controller
@RequestMapping("hr/zz-adjustment")
public class ZzAdjustmentController {
    @Autowired
    private IZzAdjustmentService adjustmentService;

    @Autowired
    private IZzEmployeeService employeeService;

    //生成员工调动记录,修改员工的部门id和职位id
    @RequestMapping("/addAdjustment")
    @ResponseBody
    //Spring MVC 默认使用 Jackson 库自动将返回值序列化为 JSON，无需手动处理,自动返回 true 或 false 的 JSON 格式。
    public boolean addAdjustment(ZzAdjustment adjustment) {
        LocalDateTime now = LocalDateTime.now();
        adjustment.setAdjustDate(now);
        try {
            adjustmentService.addAdjustment(adjustment);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //根据条件生成调动详情页面的数据
    @RequestMapping("/adjustmentInfo")
    public ModelAndView adjustmentInfo(
            @RequestParam(required = false) Integer page,@RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) Integer empid,@RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,@RequestParam(required = false) String method) {
        ModelAndView mv = new ModelAndView();
        if("searchAll".equals(method)){
            //查询所有的调动数据
            IPage<AdjustmentVo> adjustmentVoIPage = adjustmentService.pageAdjustmentVo(page,pageSize,empid,startDate,endDate);
            mv.setViewName("/hr/emp/adjustmentInfo");
            /*回填搜索信息*/
            mv.addObject("startDate",startDate);
            mv.addObject("endDate",endDate);
            mv.addObject("empId",empid);
            if(empid!=null){
                mv.addObject("empName",employeeService.getEmpVoByEid(empid).getEmpName());
            }
            /*回填搜索信息*/
            mv.addObject("adjustmentVos",adjustmentVoIPage);
            return mv;
        }
        IPage<AdjustmentVo> adjustmentVos =  adjustmentService.pageAdjustmentVoByEmpId(page,pageSize,empid,startDate,endDate);
        mv.setViewName("/hr/emp/adjustment_single");
        /*回填搜索信息*/
        mv.addObject("startDate",startDate);
        mv.addObject("endDate",endDate);
        /*回填搜索信息*/
        mv.addObject("adjustmentVosAlone",adjustmentVos);
        return mv;
    }
}
